﻿using Microsoft.AspNetCore.Mvc;
using AspNetCoreMvc.Model;
using Microsoft.AspNetCore.Mvc.ModelBinding;

namespace AspNetCoreMvc.Controllers
{
    public class ProductController : Controller
    {
        public ActionResult Index()
        {
            #region 1、视图对象返回 抽象类封装
            {
                //User user = new User() { Age = "123", Id = 1, Name = "xxx" };
                //ViewData["test"] = user;
                //return View(user);  //框架帮我们封装 
            }
            #endregion

            #region 2、视图对象返回 返回对象
            {
                //ViewResult viewResult = new ViewResult();
                //viewResult.ViewName = "Index";
                //User user = new User() { Age = "123", Id = 1, Name = "xxx" };
                //viewResult.ViewData = new Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
                //viewResult.ViewData.Model = user;
                //viewResult.ViewData["test"] = user;
                //return viewResult;
            }
            #endregion

            #region 2、视图对象返回 返回集合
            {
                ViewResult viewResult = new ViewResult();
                viewResult.ViewName = "Index";
                List<User> users = new List<User>();
                users.Add(new User() { Age = "123", Id = 1, Name = "xxx" });
                users.Add(new User() { Age = "123", Id = 1, Name = "xxx" });
                users.Add(new User() { Age = "123", Id = 1, Name = "xxx" });
                users.Add(new User() { Age = "123", Id = 1, Name = "xxx" });
                viewResult.ViewData = new Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
                viewResult.ViewData.Model = users;
                return viewResult;
            }
            #endregion
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(User user)
        {
            return RedirectToAction("Index");
        }
    }
}
